package com.kaishengit.pms.service;

import java.util.List;

import com.kaishengit.pms.dao.EmployeeDao;
import com.kaishengit.pms.entity.Employee;

public class EmployeeService {

	private EmployeeDao dao = new EmployeeDao();

	/*
	 * �û���������ѯ����¼�жϣ�
	 */
	public Employee login(String username, String password) {
		Employee em = dao.getEmployeeByNameAndPwd(username, password);
		return em;
	}

	/**
	 * �ж��û��Ƿ�����Զ���¼��cookiepwd���û���
	 * @param username
	 * @param cookiepwd
	 * @return
	 */
	public Employee getEmployee(String username, String cookiepwd) {
		Employee em = dao.findByUsernameAndCpwd(username,cookiepwd);
		return em;
	}

	/**
	 * ��ȡ��Ա�б�
	 * @return
	 */
	public List<Employee> findAllEmployees() {
		List<Employee> employees = dao.getAllEmployees();
		return employees;
	}
	


	/**
	 * ��������
	 * @param id
	 * @param password
	 */
	public void resetPwd(String id, String password) {
		//1.ͨ��id���ҹ�Ա
		Employee em = dao.findById(id);
		
		//�жϹ�Ա�Ƿ����
		if(em == null) {
			//�û�������
			throw new RuntimeException("���û������ڣ�");
		} else {
			//���ù�Ա����
			em.setPassword(password);
			dao.updateEmployee(em);
		}
	}

	//ͨ��idɾ���Ա��Ϣ(���¸�ù�Ա����)
	public void delEmployee(String id) {
		
		//1.ͨ��id���ҹ�Ա
				Employee em = dao.findById(id);
				
				//�жϹ�Ա�Ƿ����
				if(em == null) {
					//�û�������
					throw new RuntimeException("�ù�Ա�����ڣ�");
				} else {
					//���ظù�Ա
					em.setEnable(false);
					dao.updateEmployee(em);
				}
	}

	/**
	 * 得到所有雇员
	 * @return
	 */
	public List<Employee> getAllEmployees() {
		List<Employee> emps = dao.getAllEmployees();
		return emps;
	}

	/**
	 * 添加雇员
	 * @param id
	 * @param cookiepwd
	 * @param username
	 * @param password
	 */
	public void addEmployee(String id, String cookiepwd, String username,
			String password) {
				Employee e = new Employee();
		e.setId(id);
		e.setUsername(username);
		e.setPassword(password);
		e.setEnable(true);
		e.setCookiepwd(cookiepwd);
		dao.saveEmployee(e);
		
		
	}

	public List<Employee> getAllEnableEmployees() {
		
		return dao.getEnableEmployee();
	}

	
}
